﻿using System;
using System.Collections.Generic;
using System.Text;
using LBoL.Base;
using LBoL.Base.Extensions;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Cards;
using LBoL.Core.StatusEffects;
using LBoL.Core.Units;
using LBoL.EntityLib.Cards.Neutral.Blue;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;

namespace ProbeMod.Cards
{
    public sealed class ProbeShadowFuryDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            var config = GetCardDefaultConfig();

            config.TargetType = TargetType.Nobody;
            config.Type = CardType.Skill;
            config.Rarity = Rarity.Uncommon;

            config.Colors = new List<ManaColor>() { ManaColor.Black };
            config.Cost = new ManaGroup() { Black = 2 ,Any = 1 };
            config.AutoPerform = false;

            config.Damage = 12;
            config.UpgradedDamage = 15;

            config.RelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.RelativeCards = new List<string>() { "ProbeShadowFuryDeployed" };
            config.UpgradedRelativeCards = new List<string>() { "ProbeShadowFuryDeployed" };

            config.Index = CardIndexGenerator.GetUniqueIndex(config);

            return config;
        }
    }

    /// <summary>
    /// 暗影之怒
    ///    这张牌部署时，获得| 闪避 | 并立刻结束你的回合。
    ///    | 部署 |: 每当{PlayerName}闪避攻击时，对攻击源头造成 {Damage} 点伤害。
    /// </summary>
    [EntityLogic(typeof(ProbeShadowFuryDef))]
    public sealed class ProbeShadowFury : ProbeCard
    {
        public int Graze => IsUpgraded ? 3 : 2;

        public override bool RemoveFromBattleAfterPlay => false;

        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            yield return BuffAction<Graze>(level: Graze);
            //  消耗自身，给一张已部署的结界
            yield return new AddCardsToHandAction(Library.CreateCard<ProbeShadowFuryDeployed>(IsUpgraded));
            yield return new RemoveCardAction(this);

            yield return new RequestEndPlayerTurnAction();
        }
    }

    public sealed class ProbeShadowFuryDeployedDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            var config = GetCardDefaultConfig();

            config.TargetType = TargetType.Nobody;
            config.Type = CardType.Skill;
            config.Rarity = Rarity.Uncommon;
            config.IsPooled = false;
            config.Revealable = false;

            config.Colors = new List<ManaColor>() { ManaColor.Black };
            config.Cost = ManaGroup.Empty;
            config.UpgradedCost = ManaGroup.Empty;
            config.AutoPerform = true;

            config.Keywords = Keyword.Retain | Keyword.Forbidden;
            config.UpgradedKeywords = Keyword.Retain | Keyword.Forbidden;

            config.RelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeTrapKeywordSe" };

            config.Damage = 12;
            config.UpgradedDamage = 15;

            config.Index = CardIndexGenerator.GetUniqueIndex(config);

            return config;
        }
    }

    /// <summary>
    /// 暗影之怒已部署
    /// </summary>
    [EntityLogic(typeof(ProbeShadowFuryDeployedDef))]
    public sealed class ProbeShadowFuryDeployed : ProbeCard
    {
        public override bool RemoveFromBattleAfterPlay => true;

        protected override void OnEnterBattle(BattleController battle)
        {
            ReactBattleEvent(base.Battle.Player.TurnStarted, OnPlayerTurnStarting);
            ReactBattleEvent(base.Battle.Player.DamageReceived, OnPlayerDamageTaking);
        }

        private IEnumerable<BattleAction> OnPlayerDamageTaking(DamageEventArgs args)
        {
            //  闪避攻击对源头造成伤害
            if (args.DamageInfo.IsGrazed)
            {
                if (base.Zone == CardZone.Hand)
                    yield return AttackAction(args.Source);
            }
        }

        private IEnumerable<BattleAction> OnPlayerTurnStarting(UnitEventArgs args)
        {
            if (!base.Battle.BattleShouldEnd && base.Zone == CardZone.Hand)
            {
                // 回合开始，消耗自身，转化成目标卡牌
                yield return new AddCardsToDiscardAction(Library.CreateCard<ProbeShadowFury>(IsUpgraded));
                yield return new RemoveCardAction(this);
            }
        }

    }
}
